package com.car.action.api;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.car.action.util.CommonController;
import com.car.common.model.BlackList;
import com.car.common.model.MessageUnreadNum;
import com.car.common.model.NameRecord;
import com.car.common.model.PropRecord;
import com.car.common.model.Tags;
import com.car.common.model.UserAlbums;
import com.car.common.model.UserBlack;
import com.car.common.model.UserFans;
import com.car.common.model.UserFriends;
import com.car.common.model.UserSees;
import com.car.common.model.UserTags;
import com.car.common.model.UserWatch;
import com.car.common.model.Users;
import com.car.common.model.UsersInfo;
import com.car.job.TheadImgCheck;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.utils.APIJSON;
import com.utils.DateUtil;
import com.utils.PicUtil;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;
import com.utils.Toolkit;

public class UserInfoController extends CommonController {
	
    /**
     * 保存基本信息
     */
    public void save_base(){
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
			return;
		}
		String nick_name = getPara("nick_name");
		if (StringUtil.empty(nick_name)) {
			renderText(APIJSON.result(1015));
			return;
		} else if (nick_name.length() > 20){
			renderText(APIJSON.result(1016));
			return;	
		} 
		String user_header = getPara("user_header");
		if (StringUtil.empty(user_header)) {
			renderText(APIJSON.result(1017));
			return;
		}
		String sex = getPara("sex");
		if (StringUtil.empty(sex)) {
			renderText(APIJSON.result(1018));
			return;
		}
		String birthday = getPara("birthday");
		if (StringUtil.empty(birthday)) {
			renderText(APIJSON.result(1019));
			return;
		}
		String area_id = getPara("area_id");
		if (StringUtil.empty(area_id)) {
			renderText(APIJSON.result(1020));
			return;
		}
		String tag_ids = getPara("tag_ids");
		if (StringUtil.empty(tag_ids)) {
			renderText(APIJSON.result(1021));
			return;
		}
		//含有违法关键词
		if (BlackList.dao.check_content(nick_name)) {
			renderText(APIJSON.result(2007));
			return;
		}
		Long user_id = super.getUserId();
		Users users = Users.dao.findById(user_id);
		users.setNickName(StringUtil.encode(nick_name));
		try {
			users.setBirthday(DateUtil.commonDateFormat.parse(birthday).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		users.setAreaId(Long.valueOf(area_id));
		users.setTagIds(tag_ids);
		users.set("header_check", 2);
		users.update();
		NameRecord one = new NameRecord();
		one.setUserId(user_id.intValue());
		one.setNickName(nick_name);
		one.setCreateTime(SystemClock.now()/1000L);
		one.setUpdateTime(SystemClock.now()/1000L);
		one.save();
		Map<String,Object> handleData = Maps.newHashMap(); 
		handleData.put("relation_id", users.getId());
		handleData.put("img", handleData.put("img", new String[] {users.getUserHeader()}));
		handleData.put("type", 2);
		new Thread(new TheadImgCheck(handleData)).start();
		
		String[] tagIds = tag_ids.split(",");
		for(String tag : tagIds) {
			UserTags tagOne = new UserTags();
			tagOne.setTagId(Integer.valueOf(tag));
			tagOne.setUserId(user_id.intValue());
			tagOne.setCreateTime(SystemClock.now()/1000);
			tagOne.setUpdateTime(SystemClock.now()/1000);
			tagOne.save();
		}
		renderText(APIJSON.result(0));  
    }
    
    
    /**
     * 更新信息
     */
    public void update_info(){
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
			return;
		}
		String introduction = getPara("introduction");
		if (StringUtil.empty(introduction)) {
			
		} else {
			if (introduction.length() > 200){
				renderText(APIJSON.result(1016));
				return;	
			}
		}
		//含有违法关键词
		if (BlackList.dao.check_content(introduction)) {
			renderText(APIJSON.result(2007));
			return;
		}
		Long user_id = super.getUserId();
		UsersInfo info = UsersInfo.dao.findFirst("select * from users_info where user_id =" + user_id);
		String height = getPara("height");
		if (!StringUtil.empty(height)) {
			info.setHeight(getParaToInt("height"));
		}
		String weight = getPara("weight");
		if (!StringUtil.empty(weight)) {
			info.setHeight(getParaToInt("weight"));
		} 
		String orientation = getPara("orientation");
		if (!StringUtil.empty(orientation)) {
			info.setOrientation(getParaToInt("orientation"));
		} 
		
		info.setIntroduction(introduction);
		info.update();
		renderText(APIJSON.result(0));
    }
    
    /**
     * 获取头像等基本信息
     */
    public void get_base_info(){
		Long user_id = super.getUserId();
		if (!StringUtil.empty(getPara("user_id"))) {
			user_id = getParaToLong("user_id");
		}
		
		String sql = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
		sql += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + user_id;
//		System.out.println(sql);
		Users user = Users.dao.findFirst(sql);
		Map<String,Object> user_info = Users.dao.info2(user);
		Map<String,Object> result = Maps.newHashMap();
		
		result.put("user_friends", UserFriends.dao.get_friend_num(user_id.intValue()));
		result.put("user_follow", UserFans.dao.get_num(2,user_id.intValue()));
		result.put("user_fans", UserFans.dao.get_num(1,user_id.intValue()));
		result.put("user_hot", user.get("hot_val"));       // 热度
		result.put("user_free_hot", user.get("free_hot")); // 温度
		user_info.put("area_name", user_info.get("area_name") + " 距离:" + UsersInfo.dao.get_user_distance(user_id.intValue(), super.getUserId().intValue()));
		result.put("user_info", user_info);
//		result.put("user_distance", );
		
		Users users = Users.dao.findFirst("select a.id,a.is_show_nearby,a.user_bg,a.tag_ids,a.private_tag_ids,a.nick_name,a.user_header,a.sex,a.birthday,a.user_bg,c.name,b.height,b.weight,b.orientation,b.emotion,b.introduction,b.qq_openid,b.wx_unionid from users as a left join users_info as b on a.id = b.user_id left join area c on c.id = a.area_id where a.id = " + user_id +" order by a.id limit 1 ");
    	Map<String,Object> userTemp = Maps.newHashMap();
    	userTemp.put("id",               users.get("id"));
    	userTemp.put("birthday",         users.get("birthday"));
    	if (users.get("birthday") != null) {
    		userTemp.put("birthdayString",         DateUtil.commonDateFormat.format(new Date(users.getLong("birthday") * 1000L)));
    	}
    	userTemp.put("private_tag_ids",              users.get("private_tag_ids"));
    	userTemp.put("sex",              users.get("sex"));
    	userTemp.put("nick_name",        StringUtil.decode(users.getStr("nick_name")));
    	userTemp.put("name",             users.get("name"));
    	userTemp.put("height",           users.get("height"));
    	userTemp.put("weight",           users.get("weight"));
    	userTemp.put("orientation",      users.get("orientation"));
    	userTemp.put("emotion",          users.get("emotion"));
    	userTemp.put("introduction",     users.get("introduction") == null ? "" : StringUtil.decode(users.getStr("introduction")));
    	userTemp.put("qq_openid",        users.get("qq_openid") == null ? "" :users.get("qq_openid"));
    	userTemp.put("wx_unionid",       users.get("wx_unionid") == null ? "" :users.get("wx_unionid"));
    	userTemp.put("is_show_nearby",           users.get("is_show_nearby"));
    	
    	
    	List<UserWatch> userWatchList  = UserWatch.dao.find("select user_id,nick_name,user_header from user_watch where target_user_id = " + user_id +" and create_time > "+ (SystemClock.now()/1000L - 31*24 * 3600) + " order by id desc ");
    	List<Map<String,Object>> watchUsers = Lists.newArrayList();
    	List<Map<String,Object>> watchUsers_android = Lists.newArrayList();
    	for(UserWatch one : userWatchList) {
    		Map<String,Object> watch = Maps.newHashMap();
    		watch.put("user_id", 		 one.get("user_id"));
    		watch.put("nick_name", 		 one.get("nick_name"));
    		watch.put("user_header",     PicUtil.QINIU_DIR + one.get("user_header"));
    		
    		String sql1 = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql1 += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + one.get("user_id");
			Users user1 = Users.dao.findFirst(sql1);
			
    		watchUsers.add(watch);
    		watchUsers_android.add(Users.dao.info(user1,null));
    	}
    	userTemp.put("guard_num",        userWatchList == null ? 0 : userWatchList.size());
    	userTemp.put("guard_users",      watchUsers );
    	userTemp.put("guard_users_android",      watchUsers_android );
    	
    	List<Map<String,Object>> consumeUsers = Lists.newArrayList();
    	List<PropRecord> recordList = PropRecord.dao.find("SELECT sum(hot_val) AS hot_val, target_user_id FROM prop_record WHERE user_id =" + user_id +" GROUP BY target_user_id HAVING hot_val > 0 ORDER BY hot_val DESC");
		for(PropRecord one: recordList) {
			String sql1 = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql1 += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + one.get("target_user_id");
			Users user1 = Users.dao.findFirst(sql1);
			Map<String,Object> user_info1 = Users.dao.info(user1,null);
			user_info.put("hot_val", one.get("hot_val"));
			consumeUsers.add(user_info1);
		}
		userTemp.put("consume_users",      consumeUsers );
		
		
		List<PropRecord> recordList2 = PropRecord.dao.find("SELECT sum(hot_val) AS hot_val, user_id FROM prop_record WHERE target_user_id =" + user_id +" GROUP BY user_id HAVING hot_val > 0 ORDER BY hot_val DESC");
		List<Map<String,Object>> charmUsers = Lists.newArrayList();
		for(PropRecord one: recordList2) {
			String sql1 = "select u.*,ae.name as area_name, FROM_UNIXTIME(u.birthday,'%Y年%m月%d日') as birthday_string,u.is_true, CASE u.sex  WHEN '1' THEN '男' WHEN '2' THEN '女' WHEN '3' THEN 'CD' WHEN '4' THEN 'FtM' WHEN '5' THEN 'MtF'  ELSE '其他' END as sex_name ";
			sql1 += ", FROM_UNIXTIME(u.create_time,'%Y-%m-%d %H:%i') as create_time_string , FROM_UNIXTIME(u.vip_end_time,'%Y-%m-%d') as vip_end_time_string,u.auth_badge,u.talk_badge,u.vip_badpe,u.consume_badpe,u.credit  from users as u left join area ae on ae.code = u.area_id where u.id = " + one.get("user_id");
			Users user1 = Users.dao.findFirst(sql1);
			Map<String,Object> user_info1 = Users.dao.info(user1,null);
			user_info.put("hot_val", one.get("hot_val"));
			charmUsers.add(user_info1);
		}
		userTemp.put("charm_users",      charmUsers );
    	ArrayList<Tags> tagsList = CacheKit.get("constant", "tagsList", new IDataLoader(){
		       public Object load() {
		           return Tags.dao.getAll();
		}});
    	String  user_tags = users.getTagIds();
    	if (!StringUtil.empty(user_tags)) {
    		String[] tags = user_tags.split(",");
    		List<Map<String,Object>> user_tags_list = Lists.newArrayList();
    		for(int i = 0 ; i < tags.length ; i++) {
    			String id = tags[i];
    			for(Tags tag :tagsList) {
    				if(tag != null && !StringUtil.empty(id)) {
    					if (tag.getId() == new Long(id).longValue()) {
    						Map<String,Object> tagTemp = Maps.newHashMap();
    						tagTemp.put("code", tag.getId());
    						tagTemp.put("name", tag.getName());
    						user_tags_list.add(tagTemp);
    						break;
    					}
    				}
    			}
    		}
    		userTemp.put("user_tags", user_tags_list);
    	}
    	if (StringUtil.empty(users.getUserBg())) {
			userTemp.put("user_bg",   PicUtil.DEFAULT_USER_BG);
		} else {
			//  + PicUtil.QINIU_THIN
			List<UserAlbums> albumsList = UserAlbums.dao.find("select img from user_albums where status = 1 and user_id = " + user_id);
			String user_bg = PicUtil.QINIU_DIR + users.get("user_bg");
			for(UserAlbums alb :albumsList) {
				user_bg = ","  + PicUtil.QINIU_DIR + alb.getImg();
			}
			userTemp.put("user_bg",          user_bg);
		}
    	if (StringUtil.empty(users.getUserHeader())) {
			userTemp.put("user_header",      PicUtil.DEFAULT_HEAD );
		} else {
			// + PicUtil.QINIU_CUT_ADMIN
			userTemp.put("user_header",      PicUtil.QINIU_DIR + users.getUserHeader() );
		}
    	// status = 1 and
    	List<UserAlbums> albumsList = UserAlbums.dao.find("select * from user_albums where  user_id = " + user_id);
    	userTemp.put("album_num",    albumsList == null ? 0 : albumsList.size() );
    	
    	List<String> albums_list = Lists.newArrayList();
    	for(UserAlbums one:albumsList) {
    		albums_list.add(PicUtil.QINIU_DIR + one.getImg());
    	}
    	userTemp.put("albums_list",  albums_list  );
    	
    	if (user_id.longValue() != super.getUserId().longValue()) {
    		Long target_user_id = super.getUserId();
    		if (target_user_id == 0L ) {
    			result.put("is_friend", 2);
    			result.put("is_follow", 2);
    			result.put("is_black", 2);
    			result.put("is_guard", 2);
    		} else {
    			//判断是否是好友
    			UserFriends friend = UserFriends.dao.findFirst("select id from user_friends where (user_id = "+ user_id +" and target_user_id = " + target_user_id +")  or "+ " (user_id = "+ target_user_id +" and target_user_id = " + user_id +") and status = 1 and delete_time is null order by id desc limit 1 ");
    			result.put("is_friend", friend != null ? 1 :2);
    			//判断是否关注
    			UserFans fan = UserFans.dao.findFirst("select id , user_id from user_fans where  (user_id = "+ target_user_id +" and target_user_id = " + user_id +") and delete_time is null ");
    			result.put("is_follow", fan != null ? 1 :2);
    			if (fan != null) {
    				if (fan.getUserId() == 141 || fan.getUserId() == 200) {
    					result.put("is_follow", 1);
    				}
    			}
    			// 判断是否拉黑
    			UserBlack black = UserBlack.dao.findFirst("select id from user_black where status = 1 and user_id = " + target_user_id +" and black_user_id = " + user_id );
    			if (black != null) {
    				renderText(APIJSON.result(1085));
    				return;
    			}
    			result.put("is_black", black != null ? 1 :2);
    			UserWatch guard = UserWatch.dao.findFirst("select id from user_watch where user_id = " + target_user_id +" and target_user_id = " + user_id );
    			result.put("is_guard", guard != null ? 1 :2);
    			UserSees one1 = UserSees.dao.findFirst("select * from user_sees where user_id = " + user_id +" and target_user_id =" + target_user_id);
    			if (one1 == null) {
    				one1 = new UserSees();
    				one1.setUserId(target_user_id.intValue());
    				one1.setTargetUserId(user_id.intValue());
    				one1.setCreateTime(SystemClock.now()/1000L);
    				one1.setUpdateTime(SystemClock.now()/1000L);
    				if (one1.save()) {
    					MessageUnreadNum.dao.addNum(MessageUnreadNum.MESSAGE_UNREAD_TYPE_SEE, user_id.intValue());
    				}
    			}
    		}
    	} else {
    		/*
    		MessageUnreadNum one = MessageUnreadNum.dao.findFirst("select CAST(SUM(IF(type=5,num,0)) AS UNSIGNED) fans_num,CAST(SUM(IF(type=6,num,0)) AS UNSIGNED) see_num,CAST(SUM(IF(type=7,num,0)) AS UNSIGNED) order_num,CAST(SUM(IF(type=11,num,0)) AS UNSIGNED) express_num ,CAST(SUM(IF(type=8,num,0)) AS UNSIGNED) express_num from message_unread_num where user_id = " + user_id);
			if(one != null) {
				result.put("fans_num",  one.get("fans_num"));
				result.put("see_num",   one.get("see_num"));
				result.put("order_num", one.get("order_num"));
				result.put("express_num", one.get("express_num"));
				result.put("gift_num",  one.get("gift_num"));
				result.put("task_num",   0);
			}
			*/
    	}
    	
    	result.putAll(userTemp);
		renderText(APIJSON.send(result));
    }
    

    public void get_extend_info(){
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
			return;
		}
		Long user_id = super.getUserId();
		if (!StringUtil.empty(getPara("user_id"))) {
			user_id = getParaToLong("user_id");
		}
    	Users user = Users.dao.findFirst("select a.id,a.user_bg,a.tag_ids,a.nick_name,a.user_header,a.sex,a.birthday,a.user_bg,c.name,b.height,b.weight,b.orientation,b.emotion,b.introduction from users as a left join users_info as b on a.id = b.user_id left join area c on c.id = a.area_id where a.id = " + user_id +" order by a.id limit 1 ");
    	Map<String,Object> userTemp = Maps.newHashMap();
    	userTemp.put("id",               user.get("id"));
    	userTemp.put("birthday",         user.get("birthday"));
    	if (user.get("birthday") != null) {
    		userTemp.put("birthdayString",         DateUtil.commonDateFormat.format(new Date(user.getLong("birthday") * 1000L)));
    	}
    	
    	userTemp.put("sex",              user.get("sex"));
    	userTemp.put("nick_name",        user.get("nick_name"));
    	userTemp.put("name",             user.get("name"));
    	userTemp.put("height",           user.get("height"));
    	userTemp.put("weight",           user.get("weight"));
    	userTemp.put("orientation",      user.get("orientation"));
    	userTemp.put("emotion",          user.get("emotion"));
    	userTemp.put("introduction",     user.get("introduction"));
    	
    	ArrayList<Tags> tagsList = CacheKit.get("constant", "tagsList", new IDataLoader(){
		       public Object load() {
		           return Tags.dao.getAll();
		}});
    	String  user_tags = user.getTagIds();
		String[] tags = user_tags.split(",");
		List<Map<String,Object>> user_tags_list = Lists.newArrayList();
		for(int i = 0 ; i < tags.length ; i++) {
			String id = tags[i];
			for(Tags tag :tagsList) {
				if(tag != null && !StringUtil.empty(id)) {
					if (tag.getId() == new Long(id).longValue()) {
						Map<String,Object> tagTemp = Maps.newHashMap();
						tagTemp.put("code", tag.getId());
						tagTemp.put("name", tag.getName());
						user_tags_list.add(tagTemp);
						break;
					}
				}
			}
		}
		userTemp.put("user_tags", user_tags_list);
		
    	if (StringUtil.empty(user.getUserBg())) {
			userTemp.put("user_bg",     	 "" );
		} else {
			//  + PicUtil.QINIU_THIN
			userTemp.put("user_bg",          PicUtil.QINIU_DIR + user.get("user_bg"));
		}
    	if (StringUtil.empty(user.getUserHeader())) {
			userTemp.put("user_header",      PicUtil.DEFAULT_HEAD );
		} else {
			// + PicUtil.QINIU_CUT_ADMIN
			userTemp.put("user_header",      PicUtil.QINIU_DIR + user.getUserHeader() );
		}
    	renderText(APIJSON.send(userTemp));
    }
    
    /**
     * 更新用户信息
     */
    public void update(){
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
			return;
		}
		Long user_id = super.getUserId();
		Users users = Users.dao.findById(user_id);
		if (users == null) {
			users = Users.dao.findFirst("select * from users where id = " + user_id);
		}
		String nick_name = getPara("nick_name");
		if (!StringUtil.empty(nick_name)) {
			if (nick_name.length() > 8) {
				renderText(APIJSON.result(1052));
				return;
			}
			//含有违法关键词
			if (BlackList.dao.check_content(nick_name)) {
				renderText(APIJSON.result(1058));
				return;
			}
			users.setNickName(StringUtil.encode(nick_name));
			NameRecord one = new NameRecord();
			one.setUserId(user_id.intValue());
			one.setNickName(nick_name);
			one.setCreateTime(SystemClock.now()/1000L);
			one.setUpdateTime(SystemClock.now()/1000L);
			one.save();
		}
		String user_header = getPara("user_header");
		if (!StringUtil.empty(user_header)) {
			users.setUserHeader(user_header);
			users.set("header_check", 2);
			Map<String,Object> handleData = Maps.newHashMap(); 
			handleData.put("relation_id", user_id);
			handleData.put("img", new String[] {user_header});
			handleData.put("type", 2);
			new Thread(new TheadImgCheck(handleData)).start();
		}
		String user_bg = getPara("user_bg");
		if (!StringUtil.empty(user_bg)) {
			users.setUserBg(user_bg);
			users.set("bg_check", 2);
			Map<String,Object> handleData = Maps.newHashMap(); 
			handleData.put("relation_id", users.getId());
			handleData.put("img", users.getUserBg());
			handleData.put("img", new String[] {user_bg});
			handleData.put("type", 3);
			new Thread(new TheadImgCheck(handleData)).start();
		}
		String sex = getPara("sex");
		if (!StringUtil.empty(sex)) {
			users.setSex(Integer.valueOf(sex));
		}
		String birthday = getPara("birthday");
		if (!StringUtil.empty(birthday)) {
			try {
				users.setBirthday(DateUtil.commonDateFormat.parse(birthday).getTime()/1000L);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		String area_id = getPara("area_id");
		if (!StringUtil.empty(area_id)) {
			users.setAreaId(Long.valueOf(area_id));
		}
		String tag_ids = getPara("tag_ids");
		if (!StringUtil.empty(tag_ids)) {
			String[] tagIds = tag_ids.split(",");
			Db.delete("DELETE ut FROM user_tags AS ut LEFT JOIN tags AS t on ut.tag_id = t.id WHERE t.parent_id != 3 AND ut.user_id = " + user_id);
			for(String tag : tagIds) {
				UserTags one = new UserTags();
				one.setTagId(Integer.valueOf(tag));
				one.setUserId(user_id.intValue());
				one.setCreateTime(SystemClock.now()/1000);
				one.setUpdateTime(SystemClock.now()/1000);
				one.save();
			}
			users.setTagIds(tag_ids);
		}
		String private_tag_ids = getPara("private_tag_ids");
		if (!StringUtil.empty(private_tag_ids)) {
			String[] tagIds = private_tag_ids.split(",");
			Db.delete("DELETE ut FROM user_tags AS ut LEFT JOIN tags AS t on ut.tag_id = t.id WHERE t.parent_id = 3 AND ut.user_id = " + user_id);
			for(String tag : tagIds) {
				UserTags one = new UserTags();
				one.setTagId(Integer.valueOf(tag));
				one.setUserId(user_id.intValue());
				one.setCreateTime(SystemClock.now()/1000);
				one.setUpdateTime(SystemClock.now()/1000);
				one.save();
			}
			users.set("private_tag_ids",private_tag_ids);
		}
		if (users != null) {
			users.update();
		}
		
		UsersInfo info = UsersInfo.dao.findFirst("select * from users_info where user_id = " + user_id);
		boolean updateFlag = true;
		if (info == null) {
			info = new UsersInfo();
			info.setUserId(user_id.intValue());
			updateFlag = false;
		}
		String introduction = getPara("introduction");
		if (StringUtil.empty(introduction)) {
			
		} else {
			if (introduction.length() > 200){
				renderText(APIJSON.result(1053));
				return;	
			}
			//含有违法关键词
			if (BlackList.dao.check_content(introduction)) {
				renderText(APIJSON.result(1059));
				return;
			}
			info.setIntroduction(introduction);
		}
		String height = getPara("height");
		if (!StringUtil.empty(height)) {
			info.setHeight(getParaToInt("height"));
		}
		String weight = getPara("weight");
		if (!StringUtil.empty(weight)) {
			info.setWeight(getParaToInt("weight"));
		}
		String emotion = getPara("emotion");
		if (!StringUtil.empty(emotion)) {
			info.setEmotion(getParaToInt("emotion"));
		}
		String qq_openid = getPara("qq_openid");
		if (!StringUtil.empty(qq_openid)) {
			info.setQqOpenid(qq_openid);
		}
		String wx_unionid = getPara("wx_unionid");
		if (!StringUtil.empty(wx_unionid)) {
			info.setWxUnionid(wx_unionid);
		}
		String lat = getPara("lat");
		if (!StringUtil.empty(lat)) {
			info.setLat(lat);
		}
		String lng = getPara("lng");
		if (!StringUtil.empty(lng)) {
			info.setLng(lng);
		}
		if (updateFlag) {
			info.update();
		} else {
			info.save();
		}
        try {
			RongCloudUtil.update(users);
		} catch (Exception e) {
			e.printStackTrace();
		}
        renderText(APIJSON.result(0));
    }
    
    
    public void get_height(){
    	Map<String, Object> result = Maps.newHashMap();
    	result.put("unit", "cm");
    	List<Integer> array = Lists.newLinkedList();
    	for (int i = 140; i < 300; i++) {
		   array.add(i);
        }
    	result.put("height", array);
    	renderText(APIJSON.send(result));  
    }

    public void get_weight(){
    	Map<String, Object> result = Maps.newHashMap();
    	result.put("unit", "kg");
    	List<Integer> array = Lists.newLinkedList();
    	for (int i = 30; i < 150; i++) {
		   array.add(i);
        }
    	result.put("height", array);
    	renderText(APIJSON.send(result));  
    }
    
    public void get_orientation(){
    	Map<Integer, Object> result = Maps.newHashMap();
    	result.put(1, "男");
    	result.put(2, "女");
    	result.put(3, "双向");
    	renderText(APIJSON.send(result));  
    }
    
    public void get_emotion(){
    	Map<Integer, Object> result = Maps.newHashMap();
    	result.put(1, "已婚");
    	result.put(2, "单身");
    	result.put(3, "恋爱中");
    	renderText(APIJSON.send(result));  
    }
    
    /*
     * 绑定
     */
    public void bind(){
    	String openid = getPara("openid");
		if (StringUtil.empty(openid)) {
			renderText(APIJSON.result(4001));
			return;
		}
		// qq_openid wb_openid
		String type = getPara("type");
		if (!("qq".equals(type) || "wx".equals(type))) {
			renderText(APIJSON.result(4001));
			return;
		} 
		if ("qq".equals(type)) {
			type = "qq_openid";
		} else if ("wx".equals(type)) {
			type = "wx_unionid";
		}else if ("apple".equals(type)) {
			type = "apple_id";
		}
    	String message = check_user(false);
		if (!StringUtil.empty(message)) {
			renderText(message);
			return;
		} else {
			Long user_id = super.getUserId();
			Db.update("update users_info set " + type + "= '" +openid +"' where user_id = " + user_id);
		}
		renderText(APIJSON.result(0));  
		
    }
    
}



